package com.zhkj.txg.module.find.adapter

import android.graphics.Bitmap
import android.graphics.Paint
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zhkj.lib.utils.AccountManager
import com.zhkj.lib.utils.ScreenUtil
import com.zhkj.lib.utils.SpanUtil
import com.zhkj.txg.R
import com.zhkj.txg.module.find.entity.FindArticleEntity
import com.zhkj.txg.module.find.ui.FindArticleDetailActivity
import com.zhkj.txg.utils.ImageUtils
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class FindAdapter : BaseQuickAdapter<FindArticleEntity, BaseViewHolder>(R.layout.item_find), LoadMoreModule {

    private var itemWidth: Int = 0

    init {
        setOnItemClickListener { _, _, position ->
            val article = data[position]
            FindArticleDetailActivity.openDetail(context, article.id)
        }
    }

    override fun onItemViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
        itemWidth = (ScreenUtil.getScreenWidth(context) - ScreenUtil.dip(context, 30)) / 2
    }

    override fun convert(helper: BaseViewHolder, item: FindArticleEntity) {
        val ivFind = helper.setText(R.id.tvFindTitle, item.content)
            .setText(R.id.tvFindViewCount, item.visit.toString()).getView<ImageView>(R.id.ivFind)
        val subscribe =
            Observable.create<Bitmap> {
                val bitmap = ImageUtils.getCacheBitmap(context, item.images)
                it.onNext(bitmap)
            }.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe {
                ivFind.layoutParams.height = itemWidth * it.height / it.width
                ivFind.setImageBitmap(it)
            }
        // 判断是否显示商品
        helper.setGone(R.id.groupGoods, item.goods == null)
        if (item.goods != null) {
            val ivFindGoods = helper.setText(R.id.tvFindGoodsTitle, item.goods.goodsName)
                .setText(R.id.tvFindGoodsPrice, SpanUtil.setMoneySymbol(item.goods.price, true))
                .getView<ImageView>(R.id.ivFindGoods)
            ImageUtils.loadRoundCornerImage(context, ivFindGoods, item.goods.image)
            // 设置是显示市场价还是显示分享赚多少钱
            with(helper.getView<TextView>(R.id.tvFindGoodsEarn)) {
                if (AccountManager.isMember() && item.goods.shareEarn.isNotEmpty() && item.goods.shareEarn != "0") {
                    setTextColor(ContextCompat.getColor(context, R.color.colorMain))
                    paint.flags = Paint.ANTI_ALIAS_FLAG
                    text = SpanUtil.setPrefixMoneyStr(price = item.goods.shareEarn)
                } else {
                    setTextColor(ContextCompat.getColor(context, R.color.colorLightText))
                    paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
                    paint.isAntiAlias = true
                    text = SpanUtil.setMoneySymbol(item.goods.marketPrice)
                }
            }

        }
        // 判断是否有返回用户数据
        if (item.user != null) {
            val ivFindUser = helper.setText(R.id.tvFindUserName, item.user.nickname)
                .getView<ImageView>(R.id.ivFindGoodsUser)
            ImageUtils.loadCircleImage(context, ivFindUser, item.user.avatar)
        }
    }
}